package com.zone.vehicle.sdk.module.superapp;

import android.util.Log;

import com.zone.sdk.common.http.base.BaseCloudServiceManager;
import com.zone.sdk.common.http.base.Result;
import com.zone.sdk.common.http.callback.ResultCallBack;
import com.zone.vehicle.sdk.module.superapp.entity.AppUpdateEntity;
import com.zone.vehicle.sdk.module.superapp.http.SupperAppOkHttpManager;
import com.zone.vehicle.sdk.module.superapp.request.AppPushRequest;
import com.zone.vehicle.sdk.module.superapp.request.AppsDownloadRequest;
import com.zone.vehicle.sdk.module.superapp.request.AppsUpdateListRequest;
import com.zone.vehicle.sdk.module.superapp.request.SyncStatusRequest;
import com.zone.vehicle.sdk.module.superapp.response.AppPushResponse;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import io.reactivex.observers.DisposableObserver;
import okhttp3.ResponseBody;

public class CloudSuperAppServiceManager extends BaseCloudServiceManager {

    private static final String TAG = CloudSuperAppServiceManager.class.getName();

    public static CloudSuperAppServiceManager instance;

    ISuperAppCloudService iSuperAppService;

    private CloudSuperAppServiceManager(String baseUrl){
        super();
        iSuperAppService = SupperAppOkHttpManager.getInstance(baseUrl).build(ISuperAppCloudService.class);
    }

    public static CloudSuperAppServiceManager getInstance(String baseUrl) {
        if (instance == null){
            instance = new CloudSuperAppServiceManager(baseUrl);
            return instance;
        }
        return instance;
    }

    /**
     * 应用更新列表查询
     * @param request
     * @param callback
     */
    public void appsDownload(AppsDownloadRequest request, ResultCallBack<ResponseBody> callback){
        observableService.execute(iSuperAppService.appsDownload(request) , new DisposableObserver<ResponseBody>(){

            @Override
            public void onNext(@NotNull ResponseBody o) {
                Log.d(TAG, "onNext");
                callback.onSuccess(o);
            }

            @Override
            public void onError(@NotNull Throwable e) {
                Log.d(TAG, "onError "+e.toString());
                if (e == null) {
                    e = new NullPointerException("onErro called with null. Null values are generally not allowed in 2.x operators and sources.");
                }
                if (callback != null) {
                    callback.onError("", e.getMessage());
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onComplete() {
                if (!isDisposed()) {
                    try {
                        callback.onComplete();
                    } finally {
                        dispose();
                    }
                }
            }
        });
    }

    /**
     * 应用状态同步
     * @param request
     * @param callback
     */
    public void syncStatus(SyncStatusRequest request, ResultCallBack<Result> callback){
        observableService.execute(iSuperAppService.syncStatus(request) , new DisposableObserver<Result>(){

            @Override
            public void onNext(@NotNull Result o) {
                if (callback != null) {
                    callback.onSuccess(o);
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onError(@NotNull Throwable e) {
                if (e == null) {
                    e = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
                }
                if (callback != null) {
                    callback.onError("", e.getMessage());
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onComplete() {
                if (!isDisposed()) {
                    try {
                        callback.onComplete();
                    } finally {
                        dispose();
                    }
                }
            }
        });
    }

    public void appPush(AppPushRequest request, ResultCallBack<Result<AppPushResponse>> callback){
        observableService.execute(iSuperAppService.appPush(request) , new DisposableObserver<Result<AppPushResponse>>(){

            @Override
            public void onNext(@NotNull Result<AppPushResponse> o) {
                if (callback != null) {
                    callback.onSuccess(o);
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onError(@NotNull Throwable e) {
                if (e == null) {
                    e = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
                }
                if (callback != null) {
                    callback.onError("", e.getMessage());
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onComplete() {
                if (!isDisposed()) {
                    try {
                        callback.onComplete();
                    } finally {
                        dispose();
                    }
                }
            }
        });
    }

    public void updateList(AppsUpdateListRequest request, ResultCallBack<Result<List<AppUpdateEntity>>> callback){
        observableService.execute(iSuperAppService.updateList(request) , new DisposableObserver<Result<List<AppUpdateEntity>>>(){

            @Override
            public void onNext(@NotNull Result<List<AppUpdateEntity>> o) {
                if (callback != null) {
                    callback.onSuccess(o);
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onError(@NotNull Throwable e) {
                if (e == null) {
                    e = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
                }
                if (callback != null) {
                    callback.onError("", e.getMessage());
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onComplete() {
                if (!isDisposed()) {
                    try {
                        callback.onComplete();
                    } finally {
                        dispose();
                    }
                }
            }
        });
    }

}
